﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using Microhard.WarehouseInsp.Buz.Models;
using Microhard.Permission.Interface;
using Microhard.WarehouseInsp.Buz.Interfaces;
using Microhard.WarehouseInsp.Buz.Impls;
using Microsoft.Practices.Unity;
using Microhard.Core;
using Microhard.Permission.MVCFilter;
using Telerik.Web.Mvc;
using Microhard.Core.Algorithm;
using Microhard.Core.DBUtility;
using Microhard.WarehouseInsp.WebUI.Models;


namespace Microhard.WarehouseInsp.WebUI.Areas.WarehouseInsp.Controllers
{


    public class UploadController : Controller
    {
        private IFile _file = null;

        public UploadController()
        {
            _file = new FileImpl();
        }

        /// <summary>
        /// 添加到缓存
        /// </summary>
        /// <param name="PKID"></param>
        /// <param name="attachments"></param>
        /// <returns></returns>
        [MVCFilterAtrForLogon()]
        public ActionResult Save(Guid PKID, string fileType, IEnumerable<HttpPostedFileBase> attachments)
        {
            foreach (var file in attachments)
            {
                AttachmentModel model = new AttachmentModel();

                model.Name = Path.GetFileName(file.FileName);
                model.AttachmentID = Guid.NewGuid();
                model.CreateTime = DateTime.Now;
                model.MainDataID = PKID;
                if (!string.IsNullOrEmpty(fileType))
                {
                    model.FileType = fileType;
                }
                byte[] bytes = new byte[file.InputStream.Length];
                file.InputStream.Read(bytes, 0, bytes.Length);

                model.Content = bytes;
                bool doBigFile = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["UsingBigFile"]);
                if (doBigFile)
                {
                    _file.AddBigFile(model);
                }
                else
                {
                    _file.Add(model);
                }
            }
            return Content("");
        }


        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="PKID"></param>
        /// <returns></returns>
        [MVCFilterAtrForLogon()]
        public ActionResult _List(Guid PKID, string FileType)
        {
            var list = _file.Query(new AttachmentModel { MainDataID = PKID, FileType = FileType });
            var files = new GridModel()
            {
                Data = list,
                Total = list.Count
            };
            return new JsonResult
            {
                Data = files,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 下载
        /// </summary>
        /// <param name="fileID"></param>
        /// <returns></returns>
        [MVCFilterAtrForLogon()]
        public ActionResult Download(Guid fileID)
        {
            bool doBigFile = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["UsingBigFile"]);
            if (doBigFile)
            {
                var filepath = _file.GetBigFilePath(fileID);
                return File(filepath.FullFilePath, "text/comma-separated-values", HttpUtility.UrlEncode(filepath.Name));
            }
            var model = _file.GetModelByID(fileID);
            return File(model.Content, "text/comma-separated-values", HttpUtility.UrlEncode(model.Name));
        }

        /// <summary>
        /// 从数据库中移除
        /// </summary>
        /// <param name="fileID"></param>
        /// <param name="PKID"></param>
        /// <returns></returns>
        [MVCFilterAtrForLogon()]
        public ActionResult Remove(AttachmentModel model, Guid PKID)
        {
            List<Guid> id = new List<Guid>();
            id.Add(model.AttachmentID);
            _file.Delete(id);
            var list = _file.Query(new AttachmentModel { MainDataID = PKID });
            var files = new GridModel()
            {
                Data = list,
                Total = list.Count
            };
            return new JsonResult
            {
                Data = files,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

    }
}
